﻿using ckj.model;
using ckj.Models;
using Newtonsoft.Json;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Web;
using System.Web.Mvc;
using System.Web.Services.Description;

namespace ckj.Controllers
{
    public class ScanController : Controller
    {

        private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        // GET: Scan
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult QueryEQP(V_EQP v_EQP)
        {
            Logger.Info("查机台" + JsonConvert.SerializeObject(v_EQP));
            HttpData<object> httpdata = new Models.HttpData<object>();
            V_EQP jsxx = SqlSugarHelp.scope.Queryable<V_EQP>().Where(it => it.EQUIPMENTNO == v_EQP.EQUIPMENTNO).First();
            if (jsxx != null)
            {
                List<Z_YAMO> z_yamo机台未出站组件 = SqlSugarHelp.scope.Queryable<Z_YAMO>()
                                            .Where(it => it.JITAI == v_EQP.EQUIPMENTNO
                                            //&& it.GONGDANHAO == z_YAMO.GONGDANHAO
                                            //&& it.JIAOGUANHAO == z_YAMO.JIAOGUANHAO
                                            && it.CHUZHANSHIJIAN == null)
                                            .ToList();
                httpdata.data = z_yamo机台未出站组件;
                httpdata.code = 200;
            }
            else
            {
                httpdata.code = 1;
                httpdata.msg = $"未查询到{v_EQP.EQUIPMENTNO}机台信息";
            }
            return Json(httpdata);
        }      
        
        [HttpPost]
        public ActionResult DeleteInStationByCode(string jitaihao)
        {
            Logger.Info("过期删除机台:" + jitaihao);
            HttpData<List<Z_YAMO>> httpdata = new Models.HttpData<List<Z_YAMO>>();

            SqlSugarHelp.scope.Deleteable<Z_YAMO>().Where(it => it.CHUZHANSHIJIAN == null && it.JITAI == jitaihao).ExecuteCommand();

                List<Z_YAMO> z_yamo机台未出站组件 = SqlSugarHelp.scope.Queryable<Z_YAMO>()
                                            .Where(it => it.JITAI == jitaihao
                                            //&& it.GONGDANHAO == z_YAMO.GONGDANHAO
                                            //&& it.JIAOGUANHAO == z_YAMO.JIAOGUANHAO
                                            && it.CHUZHANSHIJIAN == null)
                                            .ToList();
                httpdata.data = z_yamo机台未出站组件;
                httpdata.code = 200;

            return Json(httpdata);
        }

        [HttpPost]
        public ActionResult QueryGongDan(Z_YAMO z_YAMO)
        {
            Logger.Info("查工单" + JsonConvert.SerializeObject(z_YAMO));

            HttpData<List<Z_YAMO>> httpdata = new Models.HttpData<List<Z_YAMO>>();
            V_MONO v_MONO = SqlSugarHelp.scope.Queryable<V_MONO>().Where(it => it.MONO == z_YAMO.GONGDANHAO).First();
            if (v_MONO == null)
            {
                httpdata.code = 1;
                httpdata.msg = $"v_MONO表未查到工单号{z_YAMO.GONGDANHAO}";
                return Json(httpdata);
            }

            //查出几台和工单是入站记录
            List<Z_YAMO> z_yamo机台未出站组件 = SqlSugarHelp.scope.Queryable<Z_YAMO>()
                                             .Where(it => it.JITAI == z_YAMO.JITAI
                                             //&& it.GONGDANHAO == z_YAMO.GONGDANHAO
                                             //&& it.JIAOGUANHAO == z_YAMO.JIAOGUANHAO
                                             && it.CHUZHANSHIJIAN == null)
                                             .ToList();
            //检查z的工单号和z_YAMO的工单号是否一致
            if (z_yamo机台未出站组件 != null && z_yamo机台未出站组件.Count > 0)
            {
                if (z_yamo机台未出站组件.Where(it => it.GONGDANHAO != z_YAMO.GONGDANHAO).Any())
                {
                    httpdata.code = 1;
                    httpdata.msg = $"当前机台还有工单没出站，工单号为{string.Join(",", z_yamo机台未出站组件.Select(it => it.GONGDANHAO).Distinct().ToList())}";
                    return Json(httpdata);
                }
                else
                {
                    httpdata.code = 200;
                    httpdata.data = z_yamo机台未出站组件;
                    return Json(httpdata);
                }
            }
            return Json(httpdata);
        }

        [HttpPost]
        public ActionResult QueryJiaoGuanHao(Z_YAMO z_YAMO)
        {
            Logger.Info("查胶管" + JsonConvert.SerializeObject(z_YAMO));

            HttpData<int> httpdata = new HttpData<int>();
            //查工单和胶管号是否匹配
            var 胶管工单匹配 = SqlSugarHelp.scope.Queryable<V_JSXX>().Where(it => it.ACCESSORY == z_YAMO.JIAOGUANHAO && it.PROPERTYNO == "对应工单" && it.DEFAULTVALUE == z_YAMO.GONGDANHAO).First();
            if (胶管工单匹配 ==  null)
            {
                httpdata.code = 1;
                httpdata.msg = $"工单号：{z_YAMO.GONGDANHAO}和胶管号：{z_YAMO.JIAOGUANHAO}不匹配，请检查";
                return Json(httpdata);
            }


            //V_JSXX jsxx胶水颜色 = SqlSugarHelp.scope.Queryable<V_JSXX>().Where(it => it.ACCESSORY == z_YAMO.JIAOGUANHAO && it.PROPERTYNO == "对应封装胶").First();
            //if (jsxx胶水颜色 == null)
            //{
            //    httpdata.code = 1;
            //    httpdata.msg = "没查到胶水的胶水颜色";
            //    return Json(httpdata);
            //}
            //// 根据工单查物料号
            //V_MONO v_MONO = SqlSugarHelp.scope.Queryable<V_MONO>().Where(it => it.MONO == z_YAMO.GONGDANHAO).First();

            //int 数量限制 = 99999;
            //V_CPSX v_CPSX = SqlSugarHelp.scope.Queryable<V_CPSX>()
            //                            .Where(it => it.PRODUCTNO == v_MONO.PRODUCTNO)
            //                            .First();
            //if (v_CPSX != null && int.TryParse(v_CPSX.DEFAULTVALUE, out int 数量))
            //{
            //    数量限制 = 数量;
            //}

            int 数量限制 = 查询限制数量(z_YAMO);

            httpdata.code = 200;
            httpdata.data = 数量限制;
            return Json(httpdata);
        }

        private int 查询限制数量(Z_YAMO z_YAMO)
        {
            int 数量限制 = 99999;
            int 已扫数量 = 0;
            V_JSXX v_jsxx = SqlSugarHelp.scope.Queryable<V_JSXX>().Where(it => it.ACCESSORY == z_YAMO.JIAOGUANHAO && it.PROPERTYNO == "对应配胶号").First();
            //配胶号已扫
            if (v_jsxx != null)
            {
                已扫数量 = SqlSugarHelp.scope.Queryable<Z_YAMO>().Where(it => it.PEIJIAOHAO == v_jsxx.DEFAULTVALUE).Count();
            }
            
            // 根据工单查物料号
            V_MONO v_MONO = SqlSugarHelp.scope.Queryable<V_MONO>().Where(it => it.MONO == z_YAMO.GONGDANHAO).First();
            V_CPSX v_CPSX = SqlSugarHelp.scope.Queryable<V_CPSX>()
                                        .Where(it => it.PRODUCTNO == v_MONO.PRODUCTNO)
                                        .First();
            if (v_CPSX != null && int.TryParse(v_CPSX.DEFAULTVALUE, out int 数量))
            {
                数量限制 = 数量 - 已扫数量;
            }
            return 数量限制;
        }
        [HttpPost]
        public ActionResult ScanIn(Z_YAMO z_YAMO,bool 检查过期)
        {
            Logger.Info("入站" + JsonConvert.SerializeObject(z_YAMO));
            Logger.Info("入站检查过期" + JsonConvert.SerializeObject(检查过期));
            HttpData<List<Z_YAMO>> httpdata = new HttpData<List<Z_YAMO>>();

            //组件号不能为空
            if (string.IsNullOrEmpty(z_YAMO.ZUJIANHAO))
            {
                httpdata.code = 1;
                httpdata.msg = "请扫组件号";
                return Json(httpdata);
            }

            //入站号不能为空
            if (string.IsNullOrEmpty(z_YAMO.RUZHANHAO))
            {
                httpdata.code = 1;
                httpdata.msg = "入站号不能为空，请联系开发者";
                return Json(httpdata);
            }
            //工单号不能为空
            if (string.IsNullOrEmpty(z_YAMO.GONGDANHAO))
            {
                httpdata.code = 1;
                httpdata.msg = "工单号不能为空，请联系开发者";
                return Json(httpdata);
            }
            //胶管号不能为空
            if (string.IsNullOrEmpty(z_YAMO.JIAOGUANHAO))
            {
                httpdata.code = 1;
                httpdata.msg = "胶管号不能为空，请联系开发者";
                return Json(httpdata);
            }

            //组件号有没有扫过  12.13陈柯君说进站了就不能重复扫 去掉  && it.CHUZHANSHIJIAN == null

            if (SqlSugarHelp.scope.Queryable<Z_YAMO>().Where(it => it.ZUJIANHAO == z_YAMO.ZUJIANHAO).Any())
            {
                httpdata.code = 1;
                httpdata.msg = $"{z_YAMO.ZUJIANHAO}已经扫过了，不要重复扫";
                return Json(httpdata);
            }
            string msg = "";
            //胶水大范围的过期时间
            if (string.IsNullOrEmpty(z_YAMO.ZUJIANHAO) == false)
            {
                DateTime 胶水大范围过期时间 = 查大范围过期时间( z_YAMO.ZUJIANHAO);
                if (胶水大范围过期时间 != DateTime.MinValue)
                {
                    //如果胶水大范围过期时间超过当前时间就不能报工
                    if (胶水大范围过期时间 < DateTime.Now)
                    {
                        //v_csxx   v_pzgx
                        V_PZGX v_PZGX = SqlSugarHelp.scope.Queryable<V_PZGX>().Where(it => it.COMPONENTNO == z_YAMO.ZUJIANHAO).First();
                        if (v_PZGX == null)
                        {
                            httpdata.code = 1;
                            httpdata.msg = $"组件号:{z_YAMO.ZUJIANHAO}已过期。未从V_PZGX表查到信息";
                            return Json(httpdata);
                        }
                        V_CSXX v_CSXX = SqlSugarHelp.scope.Queryable<V_CSXX>().Where(it => it.LOTNO == v_PZGX.LOTNO).First();
                        if (v_CSXX == null)
                        {
                            httpdata.code = 1;
                            httpdata.msg = $"组件号:{z_YAMO.ZUJIANHAO}已过期。" + $"组件过期时间:[{胶水大范围过期时间}],当前时间:[{DateTime.Now}]。";
                            return Json(httpdata);
                        }
                    }
                }
            }

            if (查胶水已过期(z_YAMO, 检查过期, ref msg))
            {
                httpdata.code = 1;
                httpdata.msg = msg;
                return Json(httpdata);
            }
            z_YAMO.LOTNO = SqlSugarHelp.scope.Queryable<V_YM>().Where(it => it.COMPONENTNO == z_YAMO.ZUJIANHAO).First()?.LOTNO;
            z_YAMO.ID = Guid.NewGuid().ToString();
            z_YAMO.RUZHANSHIJIAN = DateTime.Now;

            int r = SqlSugarHelp.scope.Insertable(z_YAMO).ExecuteCommand();
            if (r == 0)
            {
                httpdata.code = 1;
                httpdata.msg = "入站失败";
            }
            else
            {
                httpdata.code = 200;
                httpdata.msg = "入站成功";
            }
            List<Z_YAMO> z = SqlSugarHelp.scope.Queryable<Z_YAMO>()
                                             .Where(it => it.JITAI == z_YAMO.JITAI
                                             //&& it.GONGDANHAO == z_YAMO.GONGDANHAO
                                             //&& it.JIAOGUANHAO == z_YAMO.JIAOGUANHAO
                                             && it.CHUZHANSHIJIAN == null)
                                             .ToList();
            httpdata.data = z;
            return Json(httpdata);
        }

        public DateTime 查大范围过期时间(string ZUJIANHAO)
        {
            Logger.Info("查大范围过期时间");
            
            //开始时间
            V_RFO v_RFO = SqlSugarHelp.scope.Queryable<V_RFO>().Where(it => it.BTID == ZUJIANHAO).First();
            Logger.Info("查大范围过期时间 V_RFO:" + JsonConvert.SerializeObject(v_RFO));
            //可用分钟 永远只有一行
            V_ZZQTIME v_ZZQTIME = SqlSugarHelp.scope.Queryable<V_ZZQTIME>().First();
            Logger.Info("查大范围过期时间 V_ZZQTIME:" + JsonConvert.SerializeObject(v_ZZQTIME));

            if (v_RFO != null && v_ZZQTIME != null)
            {
                //加上分钟
               return v_RFO.TIME.Value.AddMinutes(v_ZZQTIME.VALIDMINUTE);
            }
            else
            {
                return DateTime.MinValue;
            }

        }

        public bool 查胶水已过期(Z_YAMO z_YAMO, bool 检查过期, ref string message)
        {
            Logger.Info("查胶水已过期" + JsonConvert.SerializeObject(z_YAMO));
            bool flag = false;

            V_YM v_YM = SqlSugarHelp.scope.Queryable<V_YM>().Where(it => it.COMPONENTNO == z_YAMO.ZUJIANHAO).First();
            if (v_YM == null)
            {
                flag = true;
                message = $"{z_YAMO.ZUJIANHAO}还未进入压膜站";
                return flag;
            }

            // V_YM v_YM = SqlSugarHelp.scope.Queryable<V_YM>().Where(it => it.MONO == z_YAMO.GONGDANHAO && it.COMPONENTNO == z_YAMO.ZUJIANHAO).First();
            if (v_YM.MONO != z_YAMO.GONGDANHAO)
            {
                flag = true;
                message = $"{z_YAMO.ZUJIANHAO}不属于工单{z_YAMO.GONGDANHAO}";
                return flag;
            }

            int 数量限制 = 查询限制数量(z_YAMO);
            //已扫数量
            int 已扫数量 = SqlSugarHelp.scope.Queryable<Z_YAMO>().Where(it => it.JIAOGUANHAO == z_YAMO.JIAOGUANHAO).Count();
            
            if (数量限制  <= 0)
            {
                flag = true;
                message = $"胶管号{z_YAMO.JIAOGUANHAO},可扫数量超限,剩余可扫数量{数量限制}";
                return flag;
            }

            DateTime 胶水1到期时间 = DateTime.MinValue;
            DateTime 胶水2到期时间 = DateTime.MinValue;
            if (string.IsNullOrEmpty(z_YAMO.JIAOGUANHAO) == false)
            {
                胶水1到期时间 = 胶水到期时间(z_YAMO, z_YAMO.JIAOGUANHAO);
            }

            if (string.IsNullOrEmpty(z_YAMO.JIAOGUANHAO2) == false)
            {
                胶水2到期时间 = 胶水到期时间(z_YAMO, z_YAMO.JIAOGUANHAO2);
            }

            if (胶水1到期时间 == DateTime.MinValue && 胶水2到期时间 == DateTime.MinValue)
            {
                flag = true;
                message = $"未查询到胶水过期时间，请联系开发";
                return flag;
            }

            DateTime 过期时间 = 胶水1到期时间 >= 胶水2到期时间 ? 胶水1到期时间 : 胶水2到期时间;
            if (检查过期 == true && DateTime.Now > 过期时间)
            {
                flag = true;
                message = $"组件号:{z_YAMO.ZUJIANHAO}胶水已过期。" + $"配胶到期时间:[{过期时间}]。";
                return flag;
            }
            else
            {
                return false;
            }
        }
        public bool 查胶水已过期2(Z_YAMO z_YAMO,bool 检查过期, ref string message)
        {
            Logger.Info("查胶水已过期" + JsonConvert.SerializeObject(z_YAMO));
            bool flag = false;

            V_YM v_YM = SqlSugarHelp.scope.Queryable<V_YM>().Where(it => it.COMPONENTNO == z_YAMO.ZUJIANHAO).First();
            if (v_YM == null)
            {
                flag = true;
                message = $"{z_YAMO.ZUJIANHAO}还未进入压膜站";
                return flag;
            }

            // V_YM v_YM = SqlSugarHelp.scope.Queryable<V_YM>().Where(it => it.MONO == z_YAMO.GONGDANHAO && it.COMPONENTNO == z_YAMO.ZUJIANHAO).First();
            if (v_YM.MONO != z_YAMO.GONGDANHAO)
            {
                flag = true;
                message = $"{z_YAMO.ZUJIANHAO}不属于工单{z_YAMO.GONGDANHAO}";
                return flag;
            }
            //胶水的对应配胶号
            List<V_JSXX> v_JSXXList = SqlSugarHelp.scope.Queryable<V_JSXX>().Where(it => it.ACCESSORY == z_YAMO.JIAOGUANHAO).ToList();
            if (v_JSXXList == null || v_JSXXList.Count <= 0)
            {
                flag = true;
                message = "根据胶管号从V_JSXX表没查到信息";
                return flag;
            }
            //V_JSXX jsxx应配胶号 = SqlSugarHelp.scope.Queryable<V_JSXX>().Where(it => it.ACCESSORY == z_YAMO.JIAOGUANHAO && it.PROPERTYNO == "对应配胶号").First();
            V_JSXX jsxx应配胶号 = v_JSXXList.Where(it => it.PROPERTYNO == "对应配胶号").First();
            if (jsxx应配胶号 == null)
            {
                flag = true;
                message = "没查到胶水的对应配胶号";
                return flag;
            }
            else
            {
                z_YAMO.PEIJIAOHAO = jsxx应配胶号.DEFAULTVALUE;
            }
            //胶水颜色
            //V_JSXX jsxx胶水颜色 = SqlSugarHelp.scope.Queryable<V_JSXX>().Where(it => it.ACCESSORY == z_YAMO.JIAOGUANHAO && it.PROPERTYNO == "对应封装胶").First();
            V_JSXX jsxx胶水颜色 = v_JSXXList.Where(it => it.ACCESSORY == z_YAMO.JIAOGUANHAO && it.PROPERTYNO == "对应封装胶").First();
            if (jsxx胶水颜色 == null)
            {
                flag = true;
                message = "没查到胶水的胶水颜色";
                return flag;
            }
            ////对应工单号
            //V_JSXX jsxx对应工单 = SqlSugarHelp.scope.Queryable<V_JSXX>().Where(it => it.ACCESSORY == z_YAMO.JIAOGUANHAO && it.PROPERTYNO == "对应工单").First();
            //if (jsxx对应工单 == null)
            //{
            //    flag = true;
            //    message = "没查到胶水的对应工单号";
            //    return flag;
            //}
            //根据工单查物料号
            V_MONO v_MONO = SqlSugarHelp.scope.Queryable<V_MONO>().Where(it => it.MONO == z_YAMO.GONGDANHAO).First();
            if (v_MONO == null)
            {
                flag = true;
                message = $"根据工单号{z_YAMO.GONGDANHAO}没查到胶水的对应物料号";
                return flag;
            }
            //配胶开始时间
            //V_JSXX jsxx配胶开始时间 = SqlSugarHelp.scope.Queryable<V_JSXX>().Where(it => it.ACCESSORY == z_YAMO.JIAOGUANHAO && it.PROPERTYNO == "配胶开始时间").First();
            V_JSXX jsxx配胶开始时间 = v_JSXXList.Where(it => it.ACCESSORY == z_YAMO.JIAOGUANHAO && it.PROPERTYNO == "配胶开始时间").First();
            
            if (jsxx配胶开始时间 == null)
            {
                flag = true;
                message = "没查到胶水的配胶开始时间";
                return flag;
            }

            //到 V_FZJQtime 根据 jsxx应配胶号 和 胶水颜色 查过期小时
            V_FZJQTIME v_FZJQTIME过期小时 = SqlSugarHelp.scope.Queryable<V_FZJQTIME>().Where(it => it.PACKAGINGGLUENO == jsxx胶水颜色.DEFAULTVALUE && it.PRODUCTNO == v_MONO.PRODUCTNO).First();

            if (v_FZJQTIME过期小时 == null)
            {
                flag = true;
                message = "没查到胶水的过期小时数";
                return flag;
            }
            else
            {
                string 数量限制Str = v_FZJQTIME过期小时.PACKAGINGGLUETIMELIMIT.Split('-').ToList()[1];
                if (int.TryParse(数量限制Str, out int 数量))
                {
                    //已扫数量
                    int 已扫数量 = SqlSugarHelp.scope.Queryable<Z_YAMO>().Where(it => it.JIAOGUANHAO == z_YAMO.JIAOGUANHAO).Count();
                    int 数量限制 = 数量 - 已扫数量;
                    if (数量限制 <= 0)
                    {
                        flag = true;
                        message = $"胶管号{z_YAMO.JIAOGUANHAO},可扫数量超限,限制数量{数量},已扫{已扫数量}";
                        return flag;
                    }
                }

                if (double.TryParse(v_FZJQTIME过期小时.PACKAGINGGLUETIMELIMIT.Split('-')[0], out double 过期小时数) == false)
                {
                    flag = true;
                    message = "过期小时数不符合数字格式";
                    return flag;
                }
            }


            //计算当前的时间是否在到期时间内
            if (DateTime.TryParse(jsxx配胶开始时间.DEFAULTVALUE, out DateTime 配胶时间))
            {
                double.TryParse(v_FZJQTIME过期小时.PACKAGINGGLUETIMELIMIT, out double 过期小时数);
                DateTime 到期时间 = 配胶时间.AddHours(过期小时数);
                Logger.Info($"查胶水已过期结果：组件号:{z_YAMO.ZUJIANHAO}配胶时间:{配胶时间},过期小时数:{过期小时数},到期时间:{到期时间},当前时间:{DateTime.Now}。");
                //如果当前时间超过配胶时间
                if (检查过期 == true && DateTime.Now > 到期时间)
                {
                    flag = true;
                    message = $"组件号:{z_YAMO.ZUJIANHAO}胶水已过期。" + $"配胶时间:[{配胶时间}],过期小时数:[{过期小时数}],到期时间:[{到期时间}],当前时间:[{DateTime.Now}]。";
                    return flag;
                }
                else
                {
                    flag = false;
                }
            }
            else
            {
                flag = true;
                message = "配胶开始时间不符合日期格式";
                return flag;
            }
            return flag;
        }

        public DateTime 胶水到期时间(Z_YAMO z_YAMO,string 胶管号)
        {
            List<V_JSXX> v_JSXXList = SqlSugarHelp.scope.Queryable<V_JSXX>().Where(it => it.ACCESSORY == 胶管号).ToList();
            if (v_JSXXList == null || v_JSXXList.Count <= 0)
            {
                return DateTime.MinValue;
                
            }
            V_JSXX jsxx应配胶号 = v_JSXXList.Where(it => it.PROPERTYNO == "对应配胶号").FirstOrDefault();
            if (jsxx应配胶号 == null)
            {
                return DateTime.MinValue;
            }
            else
            {
                z_YAMO.PEIJIAOHAO = jsxx应配胶号.DEFAULTVALUE;
            }

            //胶水颜色
            //V_JSXX jsxx胶水颜色 = v_JSXXList.Where(it => it.ACCESSORY == 胶管号 && it.PROPERTYNO == "对应封装胶").FirstOrDefault();
            //if (jsxx胶水颜色 == null)
            //{
            //    return DateTime.MinValue;
            //}


            //根据工单查物料号
            V_MONO v_MONO = SqlSugarHelp.scope.Queryable<V_MONO>().Where(it => it.MONO == z_YAMO.GONGDANHAO).First();
            if (v_MONO == null)
            {
                return DateTime.MinValue;
            }
         
            V_JSXX jsxx配胶开始时间 = v_JSXXList.Where(it => it.ACCESSORY == 胶管号 && it.PROPERTYNO == "配胶开始时间").FirstOrDefault();

            if (jsxx配胶开始时间 == null)
            {
                return DateTime.MinValue;
            }

            //到 V_FZJQtime 根据 jsxx应配胶号 和 胶水颜色 查过期小时
            V_FZJQTIME v_FZJQTIME过期小时 = SqlSugarHelp.scope.Queryable<V_FZJQTIME>()
                                                                .Where(it => it.PRODUCTNO == v_MONO.PRODUCTNO).First();
            //it.PACKAGINGGLUENO == jsxx胶水颜色.DEFAULTVALUE &&


            if (v_FZJQTIME过期小时 == null)
            {
                return DateTime.MinValue;
            }
            else
            {

                if (double.TryParse(v_FZJQTIME过期小时.PACKAGINGGLUETIMELIMIT.Split('-')[0], out double 过期小时数) == false)
                {
                    return DateTime.MinValue;
                }
            }
            //计算当前的时间是否在到期时间内
            if (DateTime.TryParse(jsxx配胶开始时间.DEFAULTVALUE, out DateTime 配胶时间))
            {
                double.TryParse(v_FZJQTIME过期小时.PACKAGINGGLUETIMELIMIT, out double 过期小时数);
                DateTime 到期时间 = 配胶时间.AddHours(过期小时数);
                return 到期时间;
            }
            else
            {
                return DateTime.MinValue;
            }
        }
        [HttpPost]
        public ActionResult ScanOut(Z_YAMO z_YAMO)
        {
            Logger.Info("出站" + JsonConvert.SerializeObject(z_YAMO));
            HttpData<List<Z_YAMO>> httpdata = new HttpData<List<Z_YAMO>>();
            if (string.IsNullOrEmpty(z_YAMO.ZUJIANHAO))
            {
                httpdata.code = 1;
                httpdata.msg = "请扫组件号";
                return Json(httpdata);
            }

            if (z_YAMO != null)
            {
                Z_YAMO z = SqlSugarHelp.scope.Queryable<Z_YAMO>().Where(it => it.ZUJIANHAO == z_YAMO.ZUJIANHAO).First();

                if (z == null)
                {
                    httpdata.code = 1;
                    httpdata.msg = "未查询到该组件入库记录";
                }
                else
                {
                    List<Z_YAMO> zYAMOList = SqlSugarHelp.scope.Queryable<Z_YAMO>().Where(it => it.JITAI == z.JITAI && it.CHUZHANSHIJIAN == null).ToList();
                    if (zYAMOList == null || zYAMOList.Count <= 0)
                    {
                        httpdata.code = 1;
                        httpdata.msg = "该机台没有要出站的组件";
                        return Json(httpdata);

                    }
                    int r = SqlSugarHelp.scope.Updateable<Z_YAMO>().SetColumns(it => it.CHUZHANSHIJIAN == DateTime.Now)
                        .Where(it => it.JITAI == z.JITAI && it.CHUZHANSHIJIAN == null)
                        .ExecuteCommand();
                    //string sql = $"update Z_YAMO set CHUZHANSHIJIAN = '{DateTime.Now}'";
                    //int r = SqlSugarHelp.scope.Ado.ExecuteCommand(sql);
                    if (r == 0)
                    {
                        httpdata.code = 1;
                        httpdata.msg = "出站失败";
                    }
                    else
                    {
                        httpdata.code = 200;
                        httpdata.msg = "出站成功";
                    }
                }
            }
            else
            {
                httpdata.code = 1;
                httpdata.msg = "请扫组件号";
            }
            return Json(httpdata);
        }

        [HttpPost]
        public ActionResult 胶管过期密码验证(string password)
        {
            Logger.Info("收到胶管过期密码验证:" + password);

            if (SqlSugarHelp.scope.Queryable<V_USRPE>().Where(it => it.USERNO == password).Any())
            {
                HttpData<object> httpdata = new HttpData<object>();
                httpdata.code = 200;
                httpdata.msg = "验证成功";
                return Json(httpdata);
            }
            else
            {
                HttpData<object> httpdata = new HttpData<object>();
                httpdata.code = 1;
                httpdata.msg = "验证失败,密码错误";
                return Json(httpdata);
            }
        }

        //[HttpPost]
        //public ActionResult 胶管过期密码验证(Z_YAMOEX z_YAMOEX)
        //{
        //    Logger.Info("收到胶管过期密码验证:" + JsonConvert.SerializeObject(z_YAMOEX));

        //    if (SqlSugarHelp.scope.Queryable<V_USRPE>().Where(it => it.USERNO == z_YAMOEX.PASSWORD).Any())
        //    {
        //        z_YAMOEX.ID = Guid.NewGuid().ToString();
        //        z_YAMOEX.RUZHANSHIJIAN = DateTime.Now;
        //        int r = SqlSugarHelp.scope.Insertable<Z_YAMOEX>(z_YAMOEX).ExecuteCommand();
        //        Logger.Info("塞入胶管过期密码验证:" + JsonConvert.SerializeObject(z_YAMOEX) + "。结果：" + r);

        //        HttpData<object> httpdata = new HttpData<object>();
        //        httpdata.code = 200;
        //        httpdata.msg = "验证成功";
        //        return Json(httpdata);
        //    }
        //    else
        //    {
        //        HttpData<object> httpdata = new HttpData<object>();
        //        httpdata.code = 1;
        //        httpdata.msg = "验证失败,密码错误";
        //        return Json(httpdata);
        //    }
        //}
    }
}